Visualizing Historical Yield Curves (Yield Curve Space) of Brazilian Sovereign Debt¶

Time period: 10-May-2005 to 06-Jul-2011

Import Libraries¶

In [1]:
# Import the standard data analysis libraries
import pandas as pd
import numpy as np
from scipy import stats

# Plotting library
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
matplotlib.rcParams['figure.figsize']=[14,8]
from mpl_toolkits import mplot3d # 3D library
import plotly.graph_objects as go

plot_yield_curve_heatmap¶

In [2]:
def plot_yield_curve_heatmap(series, title):
    %matplotlib inline
    ax=sns.heatmap(series, cbar_kws={'label': 'Yield(%)'})
    ticklabels = [series.index[int(tick)].strftime('%Y-%m-%d') for tick in ax.get_yticks()]
    ax.set_yticklabels(ticklabels)
    plt.title(title, fontsize=14)
    plt.show()

plot_yield_curve¶

In [3]:
def plot_yield_curve(series, date):
    %matplotlib inline
    series.plot(legend=False)
    plt.xlabel('Maturity')
    plt.ylabel('Yield (%)')
    plt.title(f"Brazilian Yield Curve as at {date}", fontsize=14)
    plt.show()

plot_yield_curve_space¶

In [4]:
def plot_yield_curve_space(series, title):
    # Set axes
    x = series.columns
    y = series.index
    z = series.to_numpy()

    # Plot
    fig = go.Figure(data=[go.Surface(z=z, x=x, y=y)])
    fig.update_layout(title=title,
                  scene = {"aspectratio": {"x": 1, "y": 1, "z": .7}})
    fig.update_layout(scene = dict(
                    xaxis_title='Maturity (yrs)',
                    yaxis_title='Date',
                    zaxis_title='Yield (%)'))
    fig.show()

Load Data and Clean¶

In [5]:
df_all = pd.read_excel('Brasilian Sovereign Curve Data.xlsx',sheet_name='Data-Total',skiprows=1)
df_all['Date'] = pd.to_datetime(df_all['Date'], format='%m/%d/%Y')
df_all = df_all.drop(['Unnamed: 0'], axis=1)
df_all.set_index('Date',inplace=True)
df_all
Out[5]:
1Y 2Y 3Y 4Y 5Y 6Y 7Y 8Y 9Y 10Y
Date
2011-06-29 0.587 0.822 1.399 1.928 NaN 2.709 NaN 3.486 3.596 4.007
2011-05-30 0.659 0.834 1.539 2.043 NaN 2.979 NaN 3.665 3.832 4.215
2011-02-27 0.718 1.259 2.176 2.698 NaN 3.662 NaN 4.215 4.341 4.675
2011-01-30 1.792 1.530 2.304 2.664 NaN 3.543 NaN 4.172 4.324 4.581
2010-12-30 0.728 0.857 1.685 2.265 2.811 3.571 NaN 4.186 4.338 4.567
2010-11-29 0.589 0.792 1.361 1.966 2.415 3.119 NaN 3.682 3.808 4.131
2010-09-29 0.486 0.498 1.009 2.027 2.339 NaN 3.053 NaN 3.446 3.723
2010-08-30 0.615 0.545 1.388 2.391 2.751 NaN 3.302 2.719 3.693 3.991
2010-06-29 0.993 1.235 2.275 3.108 3.494 NaN 4.171 3.237 4.440 4.623
2010-04-29 0.599 1.123 2.433 3.218 3.666 NaN 4.318 3.427 4.771 4.866
2010-03-30 0.549 1.296 2.577 3.259 3.743 NaN 4.451 3.457 4.809 4.959
2009-12-30 0.727 1.839 3.035 3.795 4.241 NaN 4.693 4.010 4.965 5.088
2009-11-29 1.047 1.615 NaN 2.809 3.526 3.794 NaN NaN NaN 4.783
2009-09-29 1.340 NaN 1.986 3.429 3.865 4.202 NaN NaN NaN 5.042
2009-08-30 1.426 NaN 2.234 3.493 4.320 4.667 NaN NaN NaN 5.605
2009-07-30 1.209 NaN 2.108 3.542 4.394 4.748 NaN NaN NaN 5.757
2009-06-29 1.656 NaN 3.033 4.130 4.844 5.103 NaN NaN NaN 5.928
2009-04-29 1.405 2.711 3.682 4.625 5.291 5.503 NaN NaN NaN 6.270
2009-03-30 1.823 2.955 3.822 4.887 5.350 5.783 NaN NaN NaN 6.546
2008-12-30 2.632 3.571 4.034 NaN 4.531 5.066 5.215 NaN NaN 6.142
2008-10-30 5.818 5.694 6.820 NaN 7.285 7.546 7.539 NaN NaN 8.550
2008-09-29 4.076 4.313 5.244 NaN 5.672 6.015 6.289 NaN NaN 6.662
2008-07-30 3.268 3.897 4.464 4.400 4.933 5.187 5.391 NaN NaN 5.893
2008-06-29 NaN 4.192 4.589 4.594 5.015 5.274 5.475 NaN NaN 5.756
2008-04-29 NaN 3.044 3.622 4.182 4.606 4.970 5.143 NaN 5.381 4.986
2008-03-30 NaN 2.959 3.588 4.270 4.764 5.294 5.430 NaN 5.712 5.366
2008-02-28 NaN 3.296 3.760 4.397 4.833 5.200 5.369 NaN 5.654 5.175
2008-01-30 NaN 3.522 4.118 4.367 4.516 4.782 5.217 5.383 NaN 5.706
2007-12-30 NaN 4.300 4.849 4.912 4.988 5.311 5.476 5.628 NaN 5.701
2007-11-29 NaN 4.677 4.846 4.970 5.041 5.300 5.442 5.484 NaN 5.674
2007-10-30 NaN 4.400 4.693 4.842 4.982 5.262 5.447 5.551 NaN 5.662
2007-08-30 5.540 5.238 5.582 5.612 5.685 5.716 5.826 5.887 NaN 6.059
2007-08-30 5.540 5.238 5.582 5.612 5.685 5.716 5.826 5.887 NaN 6.059
2007-07-30 5.692 5.762 5.889 6.001 6.030 6.122 6.181 6.301 NaN 6.461
2007-05-30 5.475 5.036 5.385 5.420 5.521 5.563 5.649 5.765 NaN 5.884
2007-04-29 5.019 4.652 5.220 5.262 5.323 5.391 5.494 5.530 NaN 5.723
2007-02-27 5.167 NaN 5.146 5.396 5.482 5.576 5.707 5.803 5.860 6.024
2007-01-30 5.294 NaN 5.304 5.463 5.621 5.766 5.928 6.067 6.143 6.317
2006-11-29 5.312 5.183 5.048 5.362 5.483 5.606 5.756 5.945 6.021 6.159
2006-10-30 5.182 5.244 5.109 5.488 5.575 5.703 5.901 6.093 6.179 NaN
2006-08-30 5.298 5.343 5.378 5.719 5.978 6.071 6.230 6.316 6.359 NaN
In [6]:
df_new = pd.read_excel('Brasilian Sovereign Curve Data.xlsx',sheet_name='03042023',skiprows=1)
df_new['Date'] = pd.to_datetime(df_new['Date'], format="%Y/%m/%d %H:%M:%S", errors="coerce").fillna(
    pd.to_datetime(df_new['Date'], format="%m/%d/%Y", errors="coerce"))
df_new = df_new.drop(['Unnamed: 0'], axis=1)
df_new.set_index('Date',inplace=True)
df_new
Out[6]:
1Y 2Y 3Y 4Y 6Y 8Y 10Y
Date
2023-02-04 12.910 12.037 12.158 12.160 12.548 12.649 12.768
2023-03-30 12.907 12.026 12.169 12.169 12.583 12.709 12.818
2023-03-29 12.860 11.971 12.139 12.143 12.562 12.679 12.810
2023-03-28 12.953 12.175 12.367 12.347 12.754 12.845 12.949
2023-03-27 12.808 11.997 12.205 12.206 12.625 12.772 12.914
2023-03-26 12.720 11.895 12.187 12.210 12.698 12.887 13.026
2023-03-23 12.755 11.953 12.247 12.314 12.790 12.983 13.133
2023-03-22 12.894 12.137 12.413 12.435 12.891 13.039 13.172
2023-03-21 12.734 12.066 12.336 12.341 12.783 12.932 13.048
2023-03-20 12.754 12.175 12.490 12.492 12.954 13.105 13.217
2023-03-19 12.742 12.166 12.507 12.509 12.959 13.059 13.160
2023-03-16 12.691 12.140 12.522 12.529 12.983 13.090 13.186
2023-03-15 12.761 12.239 12.633 12.645 13.093 13.179 13.257
2023-03-14 12.661 12.133 12.544 12.548 12.981 13.070 13.145
2023-03-13 12.828 12.307 12.665 12.652 13.050 13.145 13.230
2023-12-03 12.763 12.230 12.544 12.520 12.905 12.987 13.065
2023-09-03 12.938 12.440 12.734 12.697 13.078 13.188 13.247
2023-08-03 12.800 12.310 12.611 12.588 12.966 13.046 13.125
2023-07-03 12.855 12.460 12.816 12.806 13.194 13.310 13.392
2023-06-03 13.021 12.704 13.055 13.046 13.410 13.504 13.565
2023-05-03 13.111 12.781 13.144 13.125 13.495 13.575 13.623

Graphs¶

In [7]:
plot_yield_curve_heatmap(df_all, 'Brazilian Yield Curve Heatmap: 10-May-2005 to 06-Jul-2011')
In [8]:
plot_yield_curve_space(df_all, 'Brazilian Sovereign Yield Curves: 10-May-2005 to 06-Jul-2011')
In [9]:
yields=df_new.iloc[:1] 
plot_yield_curve(yields.T, '03/04/2023')